//index.js


//获取应用实例
const app = getApp()

var api = require('../../js/api.js')
var util = require('../../js/util.js');


Page({
	data: {
		hasXieyi: false,
		//是否展示授权页面
		// onOff: false,
		showModal: false, //弹窗
		userInfo: {
			avatarUrl: '../../img/wx_login.png',
			nickName: 'AAACCC'
		},
		//判断小程序的API，回调，参数，组件等是否在当前版本可用。
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		isHide: false, //true: 未授权





		motto: 'Hello World',
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo')

	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		this.onLoad()

		//当逻辑执行完后关闭刷新
		wx.stopPullDownRefresh()
	},


	// =============================弹窗 授权  start============================
	// 外面的弹窗
	btn: function() {
		this.setData({
			showModal: true
		})
	},

	// 禁止屏幕滚动
	preventTouchMove: function() {},

	// 弹出层里面的弹窗
	ok: function() {
		this.setData({
			showModal: false
		})
	},



	getPhoneNumber(e) {
		var that = this
		let msg = e.detail.errMsg
		console.log(msg)

		if ('getPhoneNumber:fail user deny' == msg) {
			console.log('====>>用户拒绝获取手机号')
		} else if ('getPhoneNumber:ok' == msg) {
			console.log('====>>用户允许获取手机号')

			var parm = e.detail
			parm.openid = app.globalData.head.openid

			api.bindPhone(parm, function(res) {
					console.log('========bind res')
					console.log(res)
					that.setData({
						showModal: false
					})
					that.toJiance()

				}, function(err) {
					console.log('========bind err')
					console.log(err)

				})

		}


	},


	// =============================弹窗 授权  end============================

	onLoad: function() {
		app.globalData.equ.name = wx.getStorageSync('equname')
		app.globalData.equ.mac = wx.getStorageSync('equmac')
		app.globalData.equ.deviceId = wx.getStorageSync('equdid')
		console.log('====>>RAM equ')
		console.log(app.globalData.equ)

		wx.getSystemInfo({
			success: function(res) {
				console.log(res.system)
				if (res.system.toUpperCase().indexOf("IOS") > -1) {
					app.globalData.isA = false
				}
			}
		})

		// =====================检查授权 start ==================
		var that = this;
		// 查看是否授权

		wx.getSetting({
			success: function(res) {
				if (res.authSetting['scope.userInfo']) {
					wx.login({
						success: function(res) {

							console.log('res1')
							console.log(res)
							wx.getUserInfo({
								success: function(res) {
									console.log('==>>查看是否授权')
									console.log('res2')
									console.log(res)
									app.globalData.userInfo = res.userInfo

									// that.setData({
									// 	userInfo: res.userInfo
									// })
									// 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
									// 根据自己的需求有其他操作再补充
									// 我这里实现的是在用户授权成功后，调用微信的 wx.login 接口，从而获取code
									api
										.wxlogin(res.encryptedData, res.iv)
										.then(res => {
											console.log('===========login============')
											that.setData({
												showModal: !res.hasPhone,
												// onOff: true
											})
											console.log(that.data.showModal)
											if (res.hasPhone) {
												that.toJiance()
											}

										})




								}
							})
						}
					})

				} else {
					// 用户没有授权
					console.log('===>>用户没有授权')
					wx.clearStorage()
					// 改变 isHide 的值，显示授权页面
					that.setData({
						isHide: true
					});
				}
			},
			fail: function(err) {

				console.log('===>>未授权')
				console.log(err)
				that.setData({
					isHide: true
				});
			}
		});

		// =====================检查授权 end==================



		if (app.globalData.userInfo) {
			this.setData({
				userInfo: app.globalData.userInfo,
				hasUserInfo: true
			})
		} else if (this.data.canIUse) {
			// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
			// 所以此处加入 callback 以防止这种情况
			app.userInfoReadyCallback = res => {
				this.setData({
					userInfo: res.userInfo,
					hasUserInfo: true
				})
			}
		} else {
			// 在没有 open-type=getUserInfo 版本的兼容处理
			wx.getUserInfo({
				success: res => {
					app.globalData.userInfo = res.userInfo
					this.setData({
						userInfo: res.userInfo,
						hasUserInfo: true
					})
				}
			})
		}

		// console.log('====缓存中的equmac')
		// console.log(wx.getStorageSync('equmac'))

	},
	getUserInfo: function(e) {
		console.log('====>>getUserInfo')
		console.log(e)
		app.globalData.userInfo = e.detail.userInfo
		this.setData({
			userInfo: e.detail.userInfo,
			hasUserInfo: true
		})
	},



	// ==================绑定用户 start==========================
	bindGetUserInfo: function(e) {
		console.log('====>>bindGetUserInfo')
		console.log(e)
		if (e.detail.userInfo) {
			//用户按了允许授权按钮
			var that = this;
			// 获取到用户的信息了，打印到控制台上看下
			console.log("用户的信息如下：");
			console.log(e.detail.userInfo);
			//为用户注册登录==============>>
			var Promise = api.wxlogin(e.detail.encryptedData, e.detail.iv)
			//授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来

			that.setData({
				userInfo: e.detail.userInfo,
				showModal: true,
				// onOff: true
				isHide: false,

			})




		} else {
			//用户按了拒绝按钮
			wx.showModal({
				title: '警告',
				content: '您拒绝了授权，将无法进入小程序，请授权之后再进入!!!',
				showCancel: false,
				confirmText: '返回授权',
				success: function(res) {
					// 用户没有授权成功，不需要改变 isHide 的值
					if (res.confirm) {
						console.log('用户点击了“返回授权”');
					}
				}
			});
		}
	},

	// ==================绑定用户 end==========================

	// =======================页面跳转 start =======================


	//事件处理函数
	bindViewTap: function() {
		wx.navigateTo({
			url: '../logs/logs'
		})
	},
	cb: function (e) {
		var that = this
		if ('cb' == e.detail.value) {
			that.setData({
				hasXieyi: true
			})
		} else {
			that.setData({
				hasXieyi: false
			})
		}

	},
	toXieyi: function (e) {
		console.log('打开协议')
		let value = e.currentTarget.dataset.val
		wx.navigateTo({
			url: "/pages/index/shuoming/shuoming?val=" + value
		})
	},

	toJiance: function() {
		//login
		api.login((res) => {
			console.log('====>>login回调')
			console.log(res)

			wx.reLaunch({
				url: '/pages/jiance/jiance'
			})
		})


	},

})
